﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WillTheySplitTheCheck;

namespace WillTheySplitTheCheck
{
    public partial class _Default : System.Web.UI.Page
    {
        cwilli46Entities restaurantDB = new cwilli46Entities();
        private Boolean refreshing = false;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void MultiSearchButton_Click(object sender, EventArgs e)
        {
            search();
        }

        private void search()
        {
            if (SearchDropDownList.SelectedItem.Text == "City")
            {
                SearchByCity();
            }
            else if (SearchDropDownList.SelectedItem.Text == "State")
            {
                SearchByState();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Zip Code")
            {
                SearchByZipCode();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Address")
            {
                SearchByAddress();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Name")
            {
                SearchByName();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Store ID")
            {
                SearchByStoreID();
            }
            else
            {

            }

            if (refreshing == false)
            {
                Label1.Visible = false;
                Label2.Visible = false;
                Label3.Visible = false;
                Label4.Visible = false;
                YesButton.Visible = false;
                NoButton.Visible = false;
                Label5.Visible = false;
            }
        }


        private void SearchByCity()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.city == MultiSearchTextBox.Text
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        private void SearchByState()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.state == MultiSearchTextBox.Text
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        private void SearchByZipCode()
        {
            try
            {
                int zip = Convert.ToInt32(MultiSearchTextBox.Text);
                var query = from p in restaurantDB.RESTAURANTS
                            where p.zip == zip
                            select p;
    
                    SearchResultsGridView.DataSource = query;
                    SearchResultsGridView.DataBind();
            }
            catch(System.FormatException fe)
            {
                fe.ToString();
            }
        }

        private void SearchByAddress()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.address == MultiSearchTextBox.Text
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        private void SearchByName()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.name.Contains(MultiSearchTextBox.Text)
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        private void SearchByStoreID()
        {
            try
            {
                int id = Convert.ToInt32(MultiSearchTextBox.Text);
                cwilli46Entities restaurantDB = new cwilli46Entities();
                var query = from p in restaurantDB.RESTAURANTS
                            where p.storeid == id
                            select p;

                SearchResultsGridView.DataSource = query;
                SearchResultsGridView.DataBind();
            }
            catch (System.FormatException fe)
            {
                fe.ToString();
            }

        }

        protected void SearchResultsGridView_SelectedIndexChanged1(object sender, EventArgs e)
        {
            GridViewRow[] rowArray = new GridViewRow[SearchResultsGridView.Rows.Count];
            SearchResultsGridView.Rows.CopyTo(rowArray, 0);

            int j = -1;
            String info = "";
            foreach (GridViewRow row in rowArray)
            {
                j++;
                if (j == SearchResultsGridView.SelectedRow.RowIndex)
                {
                    Label1.Text = info + row.Cells[1].Text;
                    Label2.Text = info + row.Cells[2].Text;
                    Label3.Text = info + row.Cells[3].Text + ", ";
                    Label3.Text = Label3.Text + row.Cells[4].Text + " ";
                    Label3.Text = Label3.Text + row.Cells[5].Text;
                }
            }

                Label1.Visible = true;
                Label2.Visible = true;
                Label3.Visible = true;
                Label4.Visible = true;
                YesButton.Visible = true;
                NoButton.Visible = true;
        }

        protected void YesButton_Click(object sender, EventArgs e)
        {
            RESTAURANT restaurant = restaurantDB.RESTAURANTS.Single(p => p.address == Label2.Text);
            restaurant.yayvotes++;
            restaurantDB.SaveChanges();
            Label5.Visible = true;
            refreshing = true;
            search();
            refreshing = false;
        }

        protected void NoButton_Click(object sender, EventArgs e)
        {
            RESTAURANT restaurant = restaurantDB.RESTAURANTS.Single(p => p.address == Label2.Text);
            restaurant.nayvotes++;
            restaurantDB.SaveChanges();
            Label5.Visible = true;
            refreshing = true;
            search();
            refreshing = false;
        }


    }
}
